/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;

import aptech.semester.code.data.ProvinceList;
import aptech.semester.code.entity.ProvinceEntity;

import com.sun.jmx.snmp.BerDecoder;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vi
 */
public class Province extends javax.swing.JInternalFrame {

    /**
     * Creates new form Position
     */
    public Province(JFrame mainFrame) {
        initComponents();
        initControls();
        txtProvinceName.setEditable(false);
        txtProvinceDescription.setEditable(false);
        btnSaveProvince.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        btnExitProvince = new javax.swing.JButton();
        btnUpdateProvince = new javax.swing.JButton();
        btnAddProvince = new javax.swing.JButton();
        btnDeleteProvince = new javax.swing.JButton();
        btnSaveProvince = new javax.swing.JButton();
        btnExportProvince = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbProvince = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtProvinceName = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtProvinceDescription = new javax.swing.JTextArea();

        setClosable(true);
        setTitle("Province");
        setPreferredSize(new java.awt.Dimension(800, 600));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Command", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        btnExitProvince.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnExitProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/exit.png"))); // NOI18N
        btnExitProvince.setText("Exit");
        btnExitProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitProvinceActionPerformed(evt);
            }
        });

        btnUpdateProvince.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnUpdateProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png"))); // NOI18N
        btnUpdateProvince.setText("Update");
        btnUpdateProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateProvinceActionPerformed(evt);
            }
        });

        btnAddProvince.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnAddProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png"))); // NOI18N
        btnAddProvince.setText("Add New");
        btnAddProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddProvinceActionPerformed(evt);
            }
        });

        btnDeleteProvince.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnDeleteProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/delete.png"))); // NOI18N
        btnDeleteProvince.setText("Delete");
        btnDeleteProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteProvinceActionPerformed(evt);
            }
        });

        btnSaveProvince.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnSaveProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/save.png"))); // NOI18N
        btnSaveProvince.setText("Save");
        btnSaveProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveProvinceActionPerformed(evt);
            }
        });

        btnExportProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report.png"))); // NOI18N
        btnExportProvince.setText("Export");
        btnExportProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportProvinceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(btnAddProvince)
                .addGap(30, 30, 30)
                .addComponent(btnSaveProvince)
                .addGap(30, 30, 30)
                .addComponent(btnUpdateProvince)
                .addGap(31, 31, 31)
                .addComponent(btnDeleteProvince)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(btnExportProvince)
                .addGap(33, 33, 33)
                .addComponent(btnExitProvince)
                .addGap(21, 21, 21))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnExitProvince)
                .addComponent(btnUpdateProvince)
                .addComponent(btnAddProvince)
                .addComponent(btnDeleteProvince)
                .addComponent(btnSaveProvince)
                .addComponent(btnExportProvince))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "List Province", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tbProvince.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "PROVINCE_NAME", "DESCRIPTION"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbProvince.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbProvinceMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbProvince);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setText("Province Name");

        jLabel2.setText("Description");

        txtProvinceDescription.setColumns(20);
        txtProvinceDescription.setRows(5);
        jScrollPane2.setViewportView(txtProvinceDescription);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(86, 86, 86)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                    .addComponent(txtProvinceName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProvinceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbProvinceMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProvinceMousePressed
        int r = tbProvince.getSelectedRow();
         if(r<0) {
            return;
        } else {
             txtProvinceName.setText(tbProvince.getValueAt(r, 1).toString());
              txtProvinceDescription.setText(tbProvince.getValueAt(r, 2).toString());
         }
        
    }//GEN-LAST:event_tbProvinceMousePressed

    private void btnAddProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddProvinceActionPerformed
        txtProvinceName.setText("");
        txtProvinceDescription.setText("");
        txtProvinceName.setEditable(true);
        txtProvinceDescription.setEditable(true);
        btnAddProvince.setEnabled(false);
        btnUpdateProvince.setEnabled(false);
        btnDeleteProvince.setEnabled(false);
        btnSaveProvince.setText("Save Add");
        btnSaveProvince.setEnabled(true);
        tbProvince.setEnabled(false);
    }//GEN-LAST:event_btnAddProvinceActionPerformed

    private void btnSaveProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveProvinceActionPerformed
        if(btnSaveProvince.getText().equals("Save Add")){
            String name = txtProvinceName.getText().trim();
            String description = txtProvinceDescription.getText().trim();
            if(name.isEmpty()){
                JOptionPane.showMessageDialog(null, "Province Name can not blank!");
                txtProvinceName.requestFocus();
                return;
            }
            if(txtProvinceName.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Province name can not mumberic!");
                txtProvinceName.requestFocus();
                return;
            }
            if(name.length() > 50){
                JOptionPane.showMessageDialog(null, "Province name less than 50 charater!");
                txtProvinceName.requestFocus();
                return;
            }
            if(txtProvinceDescription.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Description can not mumberic!");
                txtProvinceDescription.requestFocus();
                return;
            }
            
         ProvinceEntity p = new ProvinceEntity();
         p.setProvinceName(txtProvinceName.getText().trim()); 
         p.setDescription(txtProvinceDescription.getText().trim());
          
         ProvinceList pl = new ProvinceList();
         if(pl.insert(p)== true){
             JOptionPane.showMessageDialog(null, "Insert successfull");
             modelPro = (DefaultTableModel) tbProvince.getModel();
             modelPro.setRowCount(0);
             for (ProvinceEntity item : new ProvinceList().getAllProvince()) {
                 modelPro.addRow(item.toVector());
             }
             btnAddProvince.setEnabled(true);
             btnUpdateProvince.setEnabled(true);
             btnDeleteProvince.setEnabled(true);
             txtProvinceName.setEditable(false);
            txtProvinceDescription.setEditable(false);
            btnSaveProvince.setEnabled(false);
            tbProvince.setEnabled(true);
         }
        }
        
       
        if(btnSaveProvince.getText().equals("Save Update")){
            int row = tbProvince.getSelectedRow();
            String a = tbProvince.getValueAt(row, 0).toString(); 
            int ID = Integer.parseInt(a);
            
            ProvinceEntity p = new ProvinceEntity();
                p.setProvinceId(ID);
                p.setProvinceName(txtProvinceName.getText().trim());
                p.setDescription(txtProvinceDescription.getText().trim());
           
            
            ProvinceList pl = new ProvinceList();
            if (pl.update(p) == true) {
                JOptionPane.showMessageDialog(null, "Update successfull");
                modelPro = (DefaultTableModel) tbProvince.getModel();
                modelPro.setRowCount(0);
                for (ProvinceEntity item : new ProvinceList().getAllProvince()) {
                    modelPro.addRow(item.toVector());
                } 
                btnAddProvince.setEnabled(true);
                btnUpdateProvince.setEnabled(true);
                btnDeleteProvince.setEnabled(true);
                txtProvinceName.setEditable(false);
                txtProvinceDescription.setEditable(false);
                btnSaveProvince.setEnabled(false);
                tbProvince.setEnabled(true);
                
            } else {
                JOptionPane.showMessageDialog(null, "Update failed");
                return;
            }
        }
    }//GEN-LAST:event_btnSaveProvinceActionPerformed

    private void btnUpdateProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateProvinceActionPerformed
       int n = tbProvince.getSelectedRow();
       if(n < 0){
           JOptionPane.showMessageDialog(null, "Please choose row to update");
           return;
       }else{
          txtProvinceName.setEditable(true);
            txtProvinceDescription.setEditable(true);
            btnAddProvince.setEnabled(false);
            btnDeleteProvince.setEnabled(false);
            btnSaveProvince.setText("Save Update");
            btnSaveProvince.setEnabled(true);
            btnUpdateProvince.setEnabled(false);
            tbProvince.setEnabled(false);
       }
    }//GEN-LAST:event_btnUpdateProvinceActionPerformed

    private void btnDeleteProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteProvinceActionPerformed
       // lay chi so dong dc chon tren jtable
        int i = tbProvince.getSelectedRow();
     if(i<0){
         JOptionPane.showMessageDialog(null, "Please choose row to delete!");
     }else{
         int r = JOptionPane.showConfirmDialog(null, "Are you sure ?","Notify",JOptionPane.YES_NO_OPTION);
         if(r == JOptionPane.OK_OPTION){
             int id = Integer.parseInt(tbProvince.getValueAt(i, 0).toString());
             if(new ProvinceList().delete(id) == true){
                 JOptionPane.showMessageDialog(null, "Delete successfull!");
                 modelPro = (DefaultTableModel) tbProvince.getModel();
                 modelPro.setRowCount(0);
                 for (ProvinceEntity item : new ProvinceList().getAllProvince()) {
                     modelPro.addRow(item.toVector());
                 }
                 return;
             }else{
                 JOptionPane.showMessageDialog(null, "Delete failed!");
                 return;
             }
         }
     }
    }//GEN-LAST:event_btnDeleteProvinceActionPerformed

    private void btnExitProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitProvinceActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnExitProvinceActionPerformed

    private void btnExportProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportProvinceActionPerformed
        MessageFormat headerFormat= new MessageFormat("Province Report");
            MessageFormat footerFormat = new  MessageFormat("Page {0}");
        try {
            tbProvince.print(JTable.PrintMode.FIT_WIDTH,headerFormat, footerFormat);
        } catch (PrinterException ex) {
            Logger.getLogger(Bank.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnExportProvinceActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddProvince;
    private javax.swing.JButton btnDeleteProvince;
    private javax.swing.JButton btnExitProvince;
    private javax.swing.JButton btnExportProvince;
    private javax.swing.JButton btnSaveProvince;
    private javax.swing.JButton btnUpdateProvince;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbProvince;
    private javax.swing.JTextArea txtProvinceDescription;
    private javax.swing.JTextField txtProvinceName;
    // End of variables declaration//GEN-END:variables

    DefaultTableModel modelPro;
    private void initControls() {
        modelPro = (DefaultTableModel)tbProvince.getModel();
        ProvinceList pl = new ProvinceList();
        for (ProvinceEntity item : pl.getAllProvince()) {
            modelPro.addRow(item.toVector());
        }
    }
}
